package admin.controllers;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import search.DealSearchData;

@SuppressWarnings("serial")
public class ListDealServlet extends HttpServlet {

	private static final Logger log = Logger.getLogger(ListDealServlet.class
			.getName());

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		int page = 1;
		try {
			page = Integer.parseInt((String) request.getParameter("page"));
		} catch (Exception e) {
			page = 1;
		}
		DealSearchData dealSearch = new DealSearchData();
		dealSearch.setLimit(10);
		dealSearch.setPage(page);
		dealSearch.searchAdvance(null, null, null, 99);
		request.setAttribute("url", request.getRequestURL().toString());
		request.setAttribute("dealSearch", dealSearch);
		try {
			request.getRequestDispatcher("/admin/pages/list_deal.jsp").forward(
					request, response);
		} catch (ServletException e) {
			System.out.println("Error when get list Deal !");
			log.warning("Error when get list Deal !");
			log.warning(e.toString());
			e.printStackTrace();
		}
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			int page = 1;
			try {
				page = Integer.parseInt((String) request.getParameter("page"));
			} catch (Exception e) {
				page = 1;
			}
			String strDealId = request.getParameter("dealId");
			DealSearchData dealSearch = new DealSearchData();
			dealSearch.setPage(page);
			if (strDealId != null && !strDealId.isEmpty()) {
				dealSearch.searchById(strDealId);
				request.setAttribute("dealId", strDealId);
			} else {
				String strName = request.getParameter("name");
				String strCategory = request.getParameter("category");
				String strStatus = request.getParameter("status");
				int status = 0;
				try {
					status = Integer.parseInt(strStatus);
					request.setAttribute("status", strStatus);
				} catch (Exception e) {
					status = 99;
				}
				if (strCategory == null || strCategory.equals("all")) {
					strCategory = null;
				}
				dealSearch.searchAdvance(strName, null, strCategory, status);
				request.setAttribute("name", strName);
				request.setAttribute("category", strCategory);
				request.setAttribute("status", strStatus);
			}
			request.setAttribute("dealSearch", dealSearch);
			request.setAttribute("url", request.getRequestURL().toString());
			request.getRequestDispatcher("/admin/pages/list_deal.jsp").forward(
					request, response);
		} catch (Exception e) {
			System.out.println("Error when open deal list page !");
			log.warning("Error when open deal list page !");
			log.warning(e.toString());
			e.printStackTrace();
		}
	}
}
